using System;
using System.Collections.Generic;
using UnityEngine;

public class AiCustomisationController : MonoBehaviour
{
	[Serializable]
	public class PersonaModelSettings
	{
		public PersonaModel model;

		public CustomisationConfig.ShaderUseType shaderType;
	}

	public PersonaModel model;

	public UIScrollView view;

	public UITable table;

	public AiCustomisationSelectorButton customisationButton;

	private List<AiCustomisationSelectorButton> buttons = new List<AiCustomisationSelectorButton>();

	private AILooksDB.AIPlayerLook activeLook;

	public List<PersonaModelSettings> models = new List<PersonaModelSettings>();

	private void OnEnable()
	{
		InitialiseButtons();
	}

	private void InitialiseButtons()
	{
		for (int i = 0; i < AILooksDB.instance.looks.Count; i++)
		{
			if (i >= buttons.Count)
			{
				AiCustomisationSelectorButton component = NGUITools.AddChild(table.gameObject, customisationButton.gameObject).GetComponent<AiCustomisationSelectorButton>();
				component.gameObject.name = GGFormat.IndexName(i);
				buttons.Add(component);
			}
			buttons[i].Init(AILooksDB.instance.looks[i], this);
		}
		for (int j = AILooksDB.instance.looks.Count; j < buttons.Count; j++)
		{
			buttons[j].gameObject.SetActive(false);
		}
		table.Reposition();
	}

	public void SelectLook(AILooksDB.AIPlayerLook look)
	{
		activeLook = look;
		foreach (CustomisationConfig.Look lookComponent in look.lookComponents)
		{
			foreach (PersonaModelSettings model in models)
			{
				lookComponent.ApplyLook(model.model, model.shaderType);
			}
		}
		foreach (AiCustomisationSelectorButton button in buttons)
		{
			button.UpdateLookSelection(false);
		}
	}

	private void Update()
	{
		if (buttons.Count != AILooksDB.instance.looks.Count)
		{
			InitialiseButtons();
			return;
		}
		if (activeLook == null && buttons.Count > 0)
		{
			buttons[0].OnSelectLook();
			return;
		}
		foreach (CustomisationConfig.Look lookComponent in activeLook.lookComponents)
		{
			foreach (PersonaModelSettings model in models)
			{
				lookComponent.ApplyLook(model.model, model.shaderType);
			}
		}
	}
}
